#include "my_gpio.h"
#include "gtim.h"


/********************************
 * 紫色指示灯
 * 
********************************/
void purple_led_on(void)
{
    PURPLE_LED_ON();
}

void purple_led_off(void)
{
    PURPLE_LED_OFF();
}


/********************************
 * 大功率消毒灯
 * 
********************************/
void uv_led_on(void)
{
    UV_LED_ON();
}

void uv_led_off(void)
{
    UV_LED_OFF();
}

void all_led_on(void)
{
    //UV_LED_ON(); 
    UV_POWER_ON();
		UV_LED_OFF();
     PURPLE_LED_OFF();
//    GREEN_LED_ON();
}

/**
 ******************************************************************************
 ** \brief  绿灯控制
 **
 ** \return 无
 ******************************************************************************/
int green_duty = 0;
int brightness_dec_flag = 1;

void green_led_off(void)
{
    green_duty = 0;
    brightness_dec_flag = 1;
    green_led_Gtimer_duty(100);
}


void green_led_on(void)
{
    green_duty = 0;
    brightness_dec_flag = 1;
    green_led_Gtimer_duty(0);
}

/**
 ******************************************************************************
 ** \brief  绿灯控制,  duty 越大，亮度越低
 **
 ** \return 无  10m调用一次
 ******************************************************************************/
void green_led_breath(void)
{
    static int delay_cnt = 0;
    static int duty_judge_delay = 0;

    delay_cnt ++;

    if(delay_cnt%2){
        return;
    }

    // if(delay_cnt%5){
    //     return;
    // }

//-----------因为绿灯 亮度较大（50%时，就很亮），所以为了更好的达到呼吸灯效果所以采用分段增量式
#if 0
    if(brightness_dec_flag){
        green_duty += 3;
        if(green_duty > 100){
        brightness_dec_flag = 0;
    }
    }
    else{
        if(green_duty > 10){
            green_duty-=3;
        }
        else{
            brightness_dec_flag = 1;
        }
    }
#else
// 2025/04/22 覃峰是想让呼吸灯，变慢一点
// pwm duty越大，亮度越小
    if(brightness_dec_flag){

        // 00~50, 亮度较亮
        if(green_duty < 20){
            green_duty += 1;
            if(duty_judge_delay%3 == 0){
                green_duty += 1;
            }
        }
        // 50~80
        else if(green_duty < 80){
            // duty_judge_delay ++;
            // if(duty_judge_delay%2 == 0){
                green_duty += 1;
            // }
        }
        else{
            // 亮度最低
            duty_judge_delay ++;
            if(duty_judge_delay%2 == 0){
                green_duty += 1;
            }
            if(green_duty > 100){
                brightness_dec_flag = 0; 
            }
        }
    }
    else{
        // 亮度最低
        if(green_duty > 50){
            duty_judge_delay ++;
            // if(duty_judge_delay%2 == 0){
                green_duty -= 1;
            // }
        }
        else if((green_duty > 5) && (green_duty <= 50)){
            // brightness_dec_flag = 1;
            green_duty -= 2;
            duty_judge_delay = 0;
        }
        else{
            // 亮度最亮
            duty_judge_delay ++;
            if(duty_judge_delay%10 == 0){
                brightness_dec_flag = 1;
            }
        }
    }
#endif

    green_led_Gtimer_duty(green_duty);
}


/**
 ******************************************************************************
 ** \brief  初始化外部GPIO引脚
 **
 ** \return 无
 ******************************************************************************/
void green_led_init(void)
{
    stc_gpio_init_t stcChxGpioInit = {0};
    
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_PB);
    ///< GTIM_CH3(PB00)端口初始化为PWM输出
    stcChxGpioInit.u32Mode = GPIO_MODE_OUTPUT_PP;
    stcChxGpioInit.u32Pin  = GREEN_LED_PIN;
    stcChxGpioInit.u32Pull = GPIO_PULL_NONE;
    GPIOB_Init(&stcChxGpioInit);
    
    GPIO_PB00_AF_GTIM_CH3_SET();
}

#define GREEN_PWM_CNT       (40000-1)

void green_led_Gtimer_duty(int duty)
{
    if(duty > 100){
        duty = 100;
    }

    Gtim_SetCompareCaptureReg(GTIM_COMPARE_CAPTURE_CH3, GREEN_PWM_CNT *duty/100);     

}


/**
 ******************************************************************************
 ** \brief  初始化GTIMER
 **
 ** \return 无
 ******************************************************************************/
static void green_led_Gtimer_Init(void)
{
    stc_gtim_init_t    stcInitCfg = {0};    

    //配置GTIM有效，BTIM3/4/5无效
    SYSCTRL_FuncDisable(SYSCTRL_FUNC_GTIMCONFIG);
    
    ///< 使能GTIM 外设时钟
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_GTIM);
    
    stcInitCfg.u32TaskMode      = GTIM_TASK_MODE_CONTINUOUS_COUNTER; //连续计数模式
    stcInitCfg.u32WorkMode      = GTIM_WORK_MODE_PCLK;               //工作模式:定时模式，计数时钟源来自PCLK
    stcInitCfg.u32Prescaler     = GTIM_COUNTER_CLK_DIV1;             //对计数时钟进行预除频
    stcInitCfg.u32ToggleEn      = GTIM_TOGGLE_DISABLE;               //TOG输出禁止
    //stcInitCfg.u32TriggerSource = BTIM_TRIGGER_SOURCE_ETR;         //触发源选择，此样例用于内部PCLK计数，不需要配置
    //stcInitCfg.u32ExInputPolarity = GTIM_ETR_POLARITY_NORMAL;      //外部输入极性选择，此样例用于内部PCLK计数，不需要配置
    stcInitCfg.u32AutoReloadVal = 40000-1;                           //自动重载寄存ARR赋值
    Gtim_Init(&stcInitCfg);

    Gtim_SetCompareCaptureMode(GTIM_COMPARE_CAPTURE_CH3, GTIM_COMPARE_CAPTURE_PWM_NORMAL);  //CHx PWM输出，CNT＞＝CCR输出高电平
    Gtim_SetCompareCaptureReg(GTIM_COMPARE_CAPTURE_CH3, 20000);                             //比较值 20000
}

void charge_en_on(void)
{
    CHARGE_ENABLE();
}

void charge_en_off(void)
{
    CHARGE_DISABLE();
}


void led_gpio_init(void)
{
    stc_gpio_init_t stcGpioInit = {0};
    
    ///< GPIOA~D 外设时钟使能
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PA);
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PB);
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PC);
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PD);
    
    ///< PORTB端口初始化
    stcGpioInit.u32Mode = GPIO_MODE_OUTPUT_PP;
    stcGpioInit.u32Pin  = UV_LED_POWER_PIN;
    stcGpioInit.u32Pull = GPIO_PULL_NONE;
    GPIOB_Init(&stcGpioInit);

    ///< PORTC端口初始化
    stcGpioInit.u32Mode = GPIO_MODE_OUTPUT_PP;
    stcGpioInit.u32Pin  = PURPLE_LED_PIN;
    stcGpioInit.u32Pull = GPIO_PULL_NONE;
    GPIOC_Init(&stcGpioInit);

   ///< PORTD 端口初始化
    stcGpioInit.u32Mode = GPIO_MODE_OUTPUT_PP;
    stcGpioInit.u32Pin  = CHARGE_EN_PIN|UV_LED_EN_PIN;
    stcGpioInit.u32Pull = GPIO_PULL_NONE;
    GPIOD_Init(&stcGpioInit);

    green_led_init();
    green_led_Gtimer_Init();
    Gtim_Enable();   //启动gtim运行
}

